package net.kengo.volleyball.scheduler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class VolleyballSchedulerProperties
{
   private static final String CLASSNAME                 = VolleyballSchedulerProperties.class.getName();
   private static final Logger LOGGER                    = Logger.getLogger(CLASSNAME);
   private static final String PROPERTIES_FILENAME       = "VolleyballScheduler.properties";

   private static final String SCHEDULE_LOCATION_PROPERTY = "scheduleLocation";
   private static final String OUTPUT_DIRECTORY_PROPERTY  = "outputDirectory";
   private static final String TEAM_NUMBER_PROPERTY       = "teamNumber";
   
   private static final String DEFAULT_SCHEDULE_LOCATION = "schedule2012.txt";
   private static final String DEFAULT_OUTPUT_DIRECTORY  = "output";
   private static final String DEFAULT_TEAM_NUMBER       = "12";

   private Properties          properties                = new Properties();


   public VolleyballSchedulerProperties()
   {
      loadProperties();
   }

   public void loadProperties()
   {
      File propertiesFile = new File(PROPERTIES_FILENAME);
      if (propertiesFile.exists())
      {
         InputStream inputStream;
         try
         {
            inputStream = new FileInputStream(propertiesFile);
            properties.load(inputStream);
         }
         catch (FileNotFoundException e)
         {
            LOGGER.log(Level.INFO, "Unable to load file " + PROPERTIES_FILENAME);
         }
         catch (IOException e)
         {
            LOGGER.log(Level.INFO, "Unable to load properties from file");
         }
      }
   }

   public void saveProperties()
   {
      File propertiesFile = new File(PROPERTIES_FILENAME);
      try
      {
         if (!propertiesFile.exists())
         {
            propertiesFile.createNewFile();
         }
         
         OutputStream outputStream = new FileOutputStream(propertiesFile);
         properties.store(outputStream, "Volleyball Scheduler Properties");
      }
      catch (IOException e)
      {
         LOGGER.log(Level.INFO, "Unable to store properties to file");
      }
            
   }

   /**
    * @return the scheduleLocation
    */
   public String getScheduleLocation()
   {
      return properties.getProperty(SCHEDULE_LOCATION_PROPERTY, DEFAULT_SCHEDULE_LOCATION);
   }

   /**
    * @param scheduleLocation the scheduleLocation to set
    */
   public void setScheduleLocation(String scheduleLocation)
   {
      properties.setProperty(SCHEDULE_LOCATION_PROPERTY, scheduleLocation);
   }

   /**
    * @return the outputDirectory
    */
   public String getOutputDirectory()
   {
      return properties.getProperty(OUTPUT_DIRECTORY_PROPERTY, DEFAULT_OUTPUT_DIRECTORY);
   }

   /**
    * @param outputDirectory the outputDirectory to set
    */
   public void setOutputDirectory(String outputDirectory)
   {
      properties.setProperty(OUTPUT_DIRECTORY_PROPERTY, outputDirectory);
   }

   /**
    * @return the teamNumber
    */
   public String getTeamNumber()
   {
      return properties.getProperty(TEAM_NUMBER_PROPERTY, DEFAULT_TEAM_NUMBER);
   }

   /**
    * @param teamNumber the teamNumber to set
    */
   public void setTeamNumber(String teamNumber)
   {
      properties.setProperty(TEAM_NUMBER_PROPERTY, teamNumber);
   }

}
